﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    public class IDGenerateAppService : IIDGenerateAppService
    {
        private ICodeRulePropertyAppService CodeRulePropertyAppService { get; set; }
        private IManualCodeHistoryAppService ManualCodeHistoryAppService { get; set; }
        private IBaseCodeRuleAppService BaseCodeRuleAppService { get; set; }
        private IReceiptCodeRuleAppService ReceiptCodeRuleAppService { get; set; }
        private Corporation _corporation;

        public Corporation Corporation
        {
            get { return _corporation; }
            set { _corporation = value; }
        }

        public IDGenerateAppService()
        {
        }

        public IDGenerateAppService(Corporation corporation)
        {
            this._corporation = corporation;
        }

        #region IIDGenerateService Members

        public string GenerateID(SystemIdentifier.BaseCodeRuleType baseCodeRuleType)
        {
            BaseCodeRule baseCodeRule = this.BaseCodeRuleAppService.GetSingleBySearch(SearchFactory.CreateBaseCodeRuleSearch(baseCodeRuleType.GetHashCode()));
            CodeRulePropertySearch codeRulePropertySearch = SearchFactory.CreateCodeRulePropertySearch(baseCodeRule.Id, SystemIdentifier.CodeRuleType.Base.GetHashCode());
            CodeRuleProperty codeRuleProperty = this.CodeRulePropertyAppService.GetSingleBySearch(codeRulePropertySearch);

            StringBuilder idSB = new StringBuilder();
            StringBuilder idSBTemp = new StringBuilder();
            idSBTemp.Append(this._corporation.Id);
            idSBTemp.Append("_");
            idSBTemp.Append(baseCodeRule.Prefix);
            idSB.Append(idSBTemp.ToString());
            idSB.Append((codeRuleProperty.Max + 1).ToString().PadLeft(baseCodeRule.SerialNumberNumber, '0'));

            ManualCodeHistorySearch manualCodeHistorySearch = SearchFactory.CreateManualCodeHistorySearch(idSB.ToString(), SystemIdentifier.CodeRuleType.Base.GetHashCode(), baseCodeRuleType.GetHashCode());
            ManualCodeHistory manualCodeHistory = this.ManualCodeHistoryAppService.GetSingleBySearch(manualCodeHistorySearch);
            if (manualCodeHistory.IsNotNull())
            {
                codeRuleProperty.Max = codeRuleProperty.Max + 2;
                idSB.Clear();
                idSB.Append(idSBTemp.ToString());
                idSB.Append((codeRuleProperty.Max + 2).ToString().PadLeft(baseCodeRule.SerialNumberNumber, '0'));
            }
            else
            {
                codeRuleProperty.Max += 1;
            }
            this.CodeRulePropertyAppService.Update(codeRuleProperty);

            return idSB.ToString();
        }

        public string GenerateID(SystemIdentifier.ReceiptCodeRuleType receiptCodeRuleType)
        {
            ReceiptCodeRule receiptCodeRule = this.ReceiptCodeRuleAppService.GetSingleBySearch(SearchFactory.CreateReceiptCodeRuleSearch(receiptCodeRuleType.GetHashCode()));
            CodeRulePropertySearch codeRulePropertySearch = SearchFactory.CreateCodeRulePropertySearch(receiptCodeRule.Id, SystemIdentifier.CodeRuleType.Receipt.GetHashCode());
            CodeRuleProperty codeRuleProperty = this.CodeRulePropertyAppService.GetSingleBySearch(codeRulePropertySearch);

            StringBuilder idSB = new StringBuilder();
            StringBuilder idSBTemp = new StringBuilder();
            idSBTemp.Append(this._corporation.Id);
            idSBTemp.Append("_");
            idSBTemp.Append(receiptCodeRule.Prefix);
            if (receiptCodeRule.Pattern != SystemIdentifier.CodePattern.SerialNumber)
            {
                if (receiptCodeRule.YearNumber == 2)
                {
                    idSBTemp.Append(DateTime.Now.Year.ToString().Substring(2, 2));
                }
                else
                {
                    idSBTemp.Append(DateTime.Now.Year.ToString());
                }
                if (receiptCodeRule.Pattern != SystemIdentifier.CodePattern.Year)
                {
                    idSBTemp.Append(DateTime.Now.Month.ToString().PadLeft(2, '0'));
                }
                if (receiptCodeRule.Pattern == SystemIdentifier.CodePattern.Day)
                {
                    idSBTemp.Append(DateTime.Now.Day.ToString().PadLeft(2, '0'));
                }
            }

            idSB.Append(idSBTemp.ToString());
            idSB.Append((codeRuleProperty.Max + 1).ToString().PadLeft(receiptCodeRule.SerialNumberNumber, '0'));

            ManualCodeHistorySearch manualCodeHistorySearch = SearchFactory.CreateManualCodeHistorySearch(idSB.ToString(), SystemIdentifier.CodeRuleType.Receipt.GetHashCode(), receiptCodeRuleType.GetHashCode());
            ManualCodeHistory manualCodeHistory = this.ManualCodeHistoryAppService.GetSingleBySearch(manualCodeHistorySearch);
            if (manualCodeHistory.IsNotNull())
            {
                codeRuleProperty.Max = codeRuleProperty.Max + 2;
                idSB.Clear();
                idSB.Append(idSBTemp.ToString());
                idSB.Append((codeRuleProperty.Max + 2).ToString().PadLeft(receiptCodeRule.SerialNumberNumber, '0'));
            }
            else
            {
                codeRuleProperty.Max += 1;
            }
            this.CodeRulePropertyAppService.Update(codeRuleProperty);

            return idSB.ToString();
        }
        #endregion

        
    }
}
